using System;
using System.IO;
using Xceed.Words.NET;
using Xceed.Document.NET;
using System.Windows.Documents;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

namespace PatentAssistant.Utils
{
    public static class DocxExporter
    {
        /// <summary>
        /// 将文本导出为Word文档(.docx)
        /// </summary>
        /// <param name="text">要导出的文本</param>
        /// <param name="filePath">保存路径</param>
        public static void ExportToDocx(string text, string filePath)
        {
            try
            {
                using (var doc = DocX.Create(filePath))
                {
                    // 设置文档属性
                    doc.MarginTop = 72.0f;     // 1英寸 = 72点
                    doc.MarginBottom = 72.0f;
                    doc.MarginLeft = 72.0f;
                    doc.MarginRight = 72.0f;
                    
                    // 添加标题
                    var title = doc.InsertParagraph("专利交底书");
                    title.FontSize(18);
                    title.Bold();
                    title.Alignment = Alignment.center;
                    title.SpacingAfter(20);
                    
                    // 分段处理文本
                    string[] paragraphs = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
                    foreach (var paragraph in paragraphs)
                    {
                        if (string.IsNullOrWhiteSpace(paragraph)) continue;
                        
                        // 检查是否为标题行
                        if (IsPotentialTitle(paragraph))
                        {
                            var p = doc.InsertParagraph(paragraph);
                            p.FontSize(14);
                            p.Bold();
                            p.SpacingAfter(10);
                        }
                        else
                        {
                            var p = doc.InsertParagraph(paragraph);
                            p.FontSize(12);
                            p.SpacingAfter(8);
                        }
                    }
                    
                    doc.Save();
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"导出Word文档失败: {ex.Message}", ex);
            }
        }
        
        /// <summary>
        /// 将FlowDocument导出为Word文档(.docx)
        /// </summary>
        public static void ExportFlowDocumentToDocx(FlowDocument flowDoc, string filePath)
        {
            if (flowDoc == null)
                throw new ArgumentNullException(nameof(flowDoc));
                
            string text = new TextRange(flowDoc.ContentStart, flowDoc.ContentEnd).Text;
            
            // 过滤思维链内容
            text = TextUtils.RemoveChainContent(text);
            
            ExportToDocx(text, filePath);
        }
        
        /// <summary>
        /// 判断是否为标题行
        /// </summary>
        private static bool IsPotentialTitle(string text)
        {
            // 识别专利交底书的标准标题
            var titleKeywords = new[]
            {
                "技术领域",
                "背景技术", 
                "发明内容",
                "技术方案",
                "有益效果",
                "附图说明",
                "具体实施方式"
            };
            
            return titleKeywords.Any(keyword => text.Trim().Equals(keyword, StringComparison.OrdinalIgnoreCase));
        }
        

    }
} 